<html><body><div class="content"><div class="section" id="the-return-statement">
<span id="return"></span><h2>The <a class="reference internal" href="#return"><tt class="xref std std-keyword docutils literal"><span class="pre">return</span></tt></a> statement</h2>
<pre id="index-21">
<strong id="grammar-token-return_stmt">return_stmt</strong> ::=  &quot;return&quot; [<a class="reference internal" href="expressions.html#grammar-token-expression_list"><tt class="xref docutils literal"><span class="pre">expression_list</span></tt></a>]
</pre>
<p><a class="reference internal" href="#return"><tt class="xref std std-keyword docutils literal"><span class="pre">return</span></tt></a> may only occur syntactically nested in a function definition,
not within a nested class definition.</p>
<p>If an expression list is present, it is evaluated, else <tt class="docutils literal"><span class="pre">None</span></tt> is substituted.</p>
<p><a class="reference internal" href="#return"><tt class="xref std std-keyword docutils literal"><span class="pre">return</span></tt></a> leaves the current function call with the expression list (or
<tt class="docutils literal"><span class="pre">None</span></tt>) as return value.</p>
<p id="index-22">When <a class="reference internal" href="#return"><tt class="xref std std-keyword docutils literal"><span class="pre">return</span></tt></a> passes control out of a <a class="reference internal" href="compound_stmts.html#try"><tt class="xref std std-keyword docutils literal"><span class="pre">try</span></tt></a> statement with a
<a class="reference internal" href="compound_stmts.html#finally"><tt class="xref std std-keyword docutils literal"><span class="pre">finally</span></tt></a> clause, that <a class="reference internal" href="compound_stmts.html#finally"><tt class="xref std std-keyword docutils literal"><span class="pre">finally</span></tt></a> clause is executed before
really leaving the function.</p>
<p>In a generator function, the <a class="reference internal" href="#return"><tt class="xref std std-keyword docutils literal"><span class="pre">return</span></tt></a> statement indicates that the
generator is done and will cause <a class="reference internal" href="../library/exceptions.html#StopIteration" title="StopIteration"><tt class="xref py py-exc docutils literal"><span class="pre">StopIteration</span></tt></a> to be raised. The returned
value (if any) is used as an argument to construct <a class="reference internal" href="../library/exceptions.html#StopIteration" title="StopIteration"><tt class="xref py py-exc docutils literal"><span class="pre">StopIteration</span></tt></a> and
becomes the <tt class="xref py py-attr docutils literal"><span class="pre">StopIteration.value</span></tt> attribute.</p>
</div></div></body></html>